//
//  MSMainController.swift
//  weobo001
//
//  Created by mac on 10/16/15.
//  Copyright © 2015 mac. All rights reserved.
//

import UIKit

class MSMainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addChildViewControllers()
        addComposeButton()
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.bringSubviewToFront(composeButton)
    }

    private lazy var composeButton: UIButton = UIButton(image: "tabbar_compose_icon_add", backgroundImage: "tabbar_compose_button")
    
}

//加载视图
extension MSMainController{
    
    private func addComposeButton(){
        tabBar.addSubview(composeButton)
        let count = childViewControllers.count
        let w = tabBar.bounds.width / CGFloat(count) - 1
        composeButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
    }
    
    private func addChildViewControllers() {
//        设置谈Bar元素的渲染颜色z        tabBar.tintColor = UIColor.orangeColor()
        addChildViewController(MSHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MSMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(MSDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(MSProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    
    private func addChildViewController(vc: UIViewController, title: String, imageName: String ) {
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
    }
    
    
}
